package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.MusicBg;
import com.kgd.news.entity.Order;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtilsWx;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/9/26
 * @Time: 下午11:12queryForm
 */
public class MusicbgController extends BaseController {

    @Override
    public void list() {
        String title = getPara("title");

        String type = "order";
        String sql = "from music_bg where 1 = 1";
        if (!StringUtilsWx.isEmpty(title)) {
            sql += " and title like '%" + title + "%'";
            type += title;
        }
        sql += " order by id desc";
//        Page<News> data=  CacheKit.get("news", type);//type可以作为缓存的key的吧。
//        if (data == null) {
            Page<Order> data = Order.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
//            CacheKit.put("news",type,data);
//        }
        setAttr("data", data);
        render("musicbg_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        MusicBg dict = MusicBg.dao.findById(id);
        setAttr("musicbg", dict);
        if (dict != null) {
//            setAttr("isbanner", dict.getIsbanner());
        }

        render("musicbg_edit.html");
    }

    @Override
    public void save() {
        UploadFile uploadFile = getFile("img");
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        MusicBg sd = getModel(MusicBg.class, "musicbg");
//      String tempContent=  sd.getContent();
//        try {
//            String  realContent =new String(tempContent.getBytes("ISO-8859-1"),"UTF-8");
//            sd.setContent(realContent);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        if (!StringUtilsWx.isEmpty(filePath)) {
            sd.setUrl(filePath);
        }
        if(sd != null && sd.getId() != null){
            sd.update();
        }else{
            sd.save();
        }
        CacheKit.removeAll("musicbg");
        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from music_bg where id in (" + ids + ")");
        list();
    }


}
